<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace App\Components;

/**
 * Description of FileUploader
 *
 * @author Xuebing
 */
class FileUploader {
    
    public static function upload($file,$str_UploadName,$str_Folder){
        $root = config('filesystems.disks.local.root');
        if($file === null || !$file->isValid()){
            throw new \Exception('文件上传未完成！');
        }

        $extension = $file->getClientOriginalExtension();
        $str_Path = $root . '/tmp';
        $str_Name = $str_UploadName . '.' . $extension;
        if(config('app.debug')){
            $str_Name = 't_' . $str_Name;
        }
        $file->move($str_Path, $str_Name);
        $src = UpYun::upload($str_Path . '/' . $str_Name, $str_Name,$str_Folder);
        return $src;
    }
    
    public static function uploadBinary($binary,$extension,$str_UploadName,$str_Folder){
        $root = config('filesystems.disks.local.root');
        $str_Path = $root . '/tmp';
        if(!file_exists($str_Path)){
            mkdir($str_Path);
        }
        $str_Name = $str_UploadName . '.' . $extension;
        if(config('app.debug')){
            $str_Name = 't_' . $str_Name;
        }
        file_put_contents($str_Path . '/' . $str_Name, $binary);
        $src = UpYun::upload($str_Path . '/' . $str_Name, $str_Name,$str_Folder);
        unlink($str_Path . '/' . $str_Name);
        return $src;
    }
}
